import api from './api';
const app = getApp()

/* 唤起支付
 *  @param data {Object} default null 必传
 * */
const awakePay = data => {
    console.log('2222');
    return new Promise((resolve, reject) => {
        wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: 'prepay_id=' + data.prepayId,
            signType: data.signType,
            paySign: data.paySign,
            success: res => {
                console.log(res)
                resolve({
                    success: true,
                    message: '支付成功',
                    detail: res
                })
            },
            fail: (e) => {
                console.error('支付失败', e)
                reject({
                    success: false,
                    message: '支付失败',
                    detail: e
                })
            }
        })
    })
}

/* 获取订单支付所需参数
 *  @param orderId {String} default null 必传
 * */
const getPayOption = orderId => {
    return new Promise((resolve, reject) => {
        let openid = wx.getStorageSync('loginInfo').openid
        console.log('获取openid', openid)
        let url = '/api/billboard/pay/' + orderId + '/' + openid;
        api.get(url).then(res => {
            console.log(res[0])
            if (res[0].needpay === 'false') {
                resolve({
                    success: true,
                    nopay: true,
                    message: '这笔订单不需要支付',
                    detail: res[0]
                })
                return
            }
            awakePay(res[0]).then(res => {
                resolve(res)
            }).catch(e => {
                reject(e)
            })
        }).catch(e => {
            console.error(e);
            reject({
                success: false,
                message: '获取支付所需参数失败：' + (e.faultstring || '原因未知'),
                detail: e
            })
        })
    })
}

/* 获取订单支付所需参数(大排档获取支付参数地址和上面的不一样，所以复制了这个方法)
 *  @param orderId {String} default null 必传
 * */
const getPayOptionDpd = orderId => {
    return new Promise((resolve, reject) => {
        let openid = wx.getStorageSync('loginInfo').openid
        //console.log('获取openid', openid)
        let url = '/api/catering/order/pay/' + orderId + '/' + openid;
        api.get(url).then(res => {
            //console.log(res[0])
            if (res[0].needpay === 'false') {
                resolve({
                    success: true,
                    nopay: true,
                    message: '这笔订单不需要支付',
                    detail: res[0]
                })
                return
            }
            awakePay(res[0]).then(res => {
                resolve(res)
            }).catch(e => {
                reject(e)
            })
        }).catch(e => {
            //console.error(e);
            reject({
                success: false,
                message: '获取支付所需参数失败：' + (e.faultstring || '原因未知'),
                detail: e
            })
        })
    })
}


const getPayOptionXdpd = (orderId, isTakeOut = false, ver='') => {
    return new Promise((resolve, reject) => {
        let openid = wx.getStorageSync('loginInfo').openid;
        //console.log('获取openid', openid)
        let url = ''
        let oVer = ''
        if(isTakeOut) {
            url = '/api/catering/ordertakeout/payOrder';
            oVer = ver
        } else {
            url = '/api/wechatxcx/catering/order/payOrder';
            oVer = app.globalData.dpd.tableInfo.ver
        }

        api.post(url, {
            data: {
                'orderid': parseInt(orderId),
                'openid': openid,
                'ver': oVer
            }
        }).then(res => {
            if (res[0].needpay === 'false') {
                resolve({
                    success: true,
                    nopay: true,
                    message: '这笔订单不需要支付',
                    detail: res[0]
                })
                return
            }
            awakePay(res[0]).then(res => {
                resolve(res)
            }).catch(e => {
                reject(e)
            })

        }).catch(e => {
            reject({
                success: false,
                message: e.faultstring || '原因未知',
                detail: e
            })
        })
    })
}

/**
 * 大排档获取所有体质信息
 * */
const getAllPhysique = (code) => {
    return new Promise((resolve, reject)=> {
        wx.request({
            method: 'POST',
            url: 'https://lk-xchx-food-suggest-api-pro.op.laikang.com/api/v1/index/getAllPhysique',
            success:(res)=> {
                if (res.data.code == 200) {
                    if (res.data.data.length > 0){
                        let physiqueList = res.data.data
                        const  physiqueMsg =   physiqueList.filter((item) =>{
                            return  item.code === code
                        })
                        resolve(physiqueMsg)
                    }
                }else {
                    reject('接口报错', res.data)
                }
            },
            fail:(res) => {
                wx.hideLoading()
                reject(res)
            }
        })
    })
}


export default {
    // 获取物流列表
    apiGetWuliu(orderId) {
        if (!orderId) {
            return Promise.reject('没有传入订单ID')
        }
        return new Promise((resolve, reject) => {
            api.get('/api/boardorder/logistics/' + orderId).then(res => {
                let list = res[0].progress.reverse()
                if (list.length === 0) {
                    resolve(list)
                    return
                }
                for (let i of list) {
                    i.a = i.time.substring(5, 10)
                    i.b = i.time.substring(11, 16)
                }
                resolve(list)
            }).catch(e => {
                console.error(e)
                reject(e)
            })
        })
    },
    // 确认收货和取消订单
    apiOkOrderShAndQxdd(options = {
        url: null,
        text: null
    }) {
        let {
            url,
            text
        } = options
        return new Promise((resolve, reject) => {
            wx.showModal({
                title: '提示',
                content: `您确定要${text}吗？`,
                success: res => {
                    if (res.confirm) {
                        api.get(url).then(res => {
                            console.log(res)
                            wx.showToast({
                                title: `${text}成功!`,
                            })
                            resolve(res)
                        }).catch(e => {
                            console.log(e)
                            wx.showToast({
                                icon: 'none',
                                title: `${text}失败!`,
                            })
                            reject(e)
                        })
                    }
                }
            })
        })
    },
    // 支付订单
    apiOrderPay: getPayOption,
    apiOrderPayDpd: getPayOptionDpd,
    apiOrderPayXdpd: getPayOptionXdpd,
    getAllPhysique:getAllPhysique
}
